package com.hongxin.nw.netty.model.analysis;

import com.hongxin.nw.netty.utils.BinaryTools;
import com.hongxin.nw.netty.utils.FieldAnalysis;
import com.hongxin.nw.netty.values.TypeIdentif;
import io.netty.buffer.ByteBuf;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * @remark --
 * @author: GuoXiaolin
 * @date 2019/5/22 19:03
 * @Version 1.0
 * @Descibe
 */
@Slf4j
@Getter
@Setter
public class ASDUstart {

    private String start;//    起始标识	1Byte	BIN码	固定68H
    private String length;//    帧长度	2Byte	BIN码
    private String index;//    协议启动标识帧	1Byte	BIN码	固定位“FF”
    private String version;//    协议版本	1Byte	压缩BCD码	保留为02
    private String billingid;//    计费模型ID	8Byte	BIN码
    private String code;//    设备编号	8Byte	压缩BCD码	充电设备编号
    private String num;//    充电接口数量	1Byte	压缩BCD码
    private String model;//    充电模式	1Byte	BIN码	充电设备支持的充电模式，位表示，1表示支持，0 表示不支持，D0：按电量；D1：按时间；D2：自动；D3： 按金额；D4~D7保留
    private String statAddress;//    站地址	2Byte	压缩BCD码	站地址

    private int idx=0;
    private ByteBuf byteBuf;

    private ASDUstart subStart() {
        int byteLength = TypeIdentif.BYTE_START.getInt();
        this.start = BinaryTools.subByteBuf(byteLength,idx, byteBuf);
        this.idx += byteLength;
        return this;
    }

    private ASDUstart subLength() {
        int byteLength = TypeIdentif.BYTE_FRAME_LENGTH.getInt();
        this.length = BinaryTools.subByteBuf(byteLength,idx, byteBuf);
        this.length = FieldAnalysis.analysisBin(this.length);
        this.idx += byteLength;
        return this;
    }

    private ASDUstart subIndex() {
        int byteLength = TypeIdentif.BYTE_AGREEMENT_VERSION.getInt();
        this.index = BinaryTools.subByteBuf(byteLength,idx, byteBuf);
        this.index = FieldAnalysis.analysisBin(this.index);
        this.idx += byteLength;
        return this;
    }

    private ASDUstart subVersion() {
        int byteLength = TypeIdentif.BYTE_AGREEMENT_VERSION.getInt();
        this.version = BinaryTools.subByteBuf(byteLength,idx, byteBuf);
        this.version = FieldAnalysis.analysisBCD(this.version);
        this.idx += byteLength;
        return this;
    }

    private ASDUstart subBillingID() {
//        int byteLength = TypeIdentif.BYTE_BILLING_MODEL_ID.getInt();
//        this.billingid = BinaryTools.subByteBuf(byteLength,idx, byteBuf);
//        this.billingid = FieldAnalysis.analysisBCD(this.billingid);
//        this.idx += byteLength;


        this.billingid = FieldAnalysis.subBill(TypeIdentif.BYTE_BILLING_MODEL_ID.getInt(),idx,byteBuf);
        this.idx += TypeIdentif.BYTE_BILLING_MODEL_ID.getInt();
        return this;
    }

    private ASDUstart subCode() {
        int byteLength = TypeIdentif.BYTE_CODE.getInt();
        this.code = BinaryTools.subByteBuf(byteLength,idx, byteBuf);
        this.code = FieldAnalysis.analysisBCD(this.code);
        this.idx += byteLength;
        return this;
    }

    private ASDUstart subNum() {
        int byteLength = TypeIdentif.BYTE_CHARGE_INTER_NUM.getInt();
        this.num = BinaryTools.subByteBuf(byteLength,idx, byteBuf);
        this.num = FieldAnalysis.analysisBCD(this.num);
        this.idx += byteLength;
        return this;
    }

    private ASDUstart subModel() {
        int byteLength = TypeIdentif.BYTE_CHARGE_MODEL.getInt();
        this.model = BinaryTools.subByteBuf(byteLength,idx, byteBuf);
        this.model = FieldAnalysis.analysisBin(this.model);
        this.idx += byteLength;
        return this;
    }

    private ASDUstart subStatAddress() {
        int byteLength = TypeIdentif.BYTE_STAT_ADDRESS.getInt();
        this.statAddress = BinaryTools.subByteBuf(byteLength,idx, byteBuf);
        this.statAddress = FieldAnalysis.analysisBCD(this.statAddress);
        this.idx += byteLength;
        return this;
    }

    public void analysisStr(ByteBuf byteBuf){
        this.byteBuf = byteBuf;
        this.subStart()
                .subLength()
                .subIndex()
                .subVersion()
                .subBillingID()
                .subCode()
                .subNum()
                .subModel()
                .subStatAddress();
    }

    @Override
    public String toString() {
        return "ASDUstart{" +
                "start=" + start +
                ", length=" + length +
                ", index='" + index + '\'' +
                ", version='" + version + '\'' +
                ", billingid=" + billingid +
                ", code=" + code +
                ", num=" + num +
                ", model='" + model + '\'' +
                ", statAddress=" + statAddress +
                '}';
    }
}
